package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.CurriculumAssistVO;
import edu.zqu.instructionalMatters.project.entity.CurriculumAssist;
import edu.zqu.instructionalMatters.project.mapper.CurriculumAssistMapper;
import edu.zqu.instructionalMatters.project.service.CollegeService;
import edu.zqu.instructionalMatters.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 课表-助教 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "课表-助教")
public class CurriculumAssistController {
    @Autowired
    private CurriculumAssistMapper curriculumAssistMapper;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取课表-助教信息列表")
    @RequestMapping(value = "/curriculumAssists", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "curriculumId", value = "课程id", paramType = "query", required = false),
            @ApiImplicitParam(name = "assistId", value = "助教id", paramType = "query", required = false)
    })
    public ZQUResult get(BigInteger collegeId,
                         BigInteger curriculumId,
                         BigInteger assistId) {
        CurriculumAssist curriculumAssist1 = new CurriculumAssist()
                .setCollegeId(collegeId).setCurriculumId(curriculumId).setAssistId(assistId);                           //合成CurriculumAssist对象
        List<CurriculumAssist> list1 = curriculumAssistMapper.findAll(curriculumAssist1);                               //获取 List<CurriculumAssist>
        List<CurriculumAssistVO> list = new ArrayList<>();                                                              //初始化 List<CurriculumAssistVO>
        for (CurriculumAssist curriculumAssist : list1) {                                                                //循环遍历
            CurriculumAssistVO curriculumAssistVO = new CurriculumAssistVO();                                           //初始化 CurriculumAssistVO
            BeanUtils.copyProperties(curriculumAssist, curriculumAssistVO);                                              //对象复制赋值
            String collegeName = collegeService.getCollegeName(curriculumAssist.getCollegeId());                        //获取学校名称
            String assistName = userService.getUserName(curriculumAssist.getAssistId());                                //获取助教名称
            curriculumAssistVO.setCollegeName(collegeName);                                                             //设置学校名称
            curriculumAssistVO.setAssistName(assistName);                                                               //设置助教名称
            list.add(curriculumAssistVO);                                                                               //加入到 list
        }
        return ZQUResult.success(list);                                                                                 //成功返回 list
    }

    @ApiOperation(value = "获取课表-助教信息", response = CurriculumAssistVO.class)
    @RequestMapping(value = "/curriculumAssist/{id}", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "课表-助教id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        CurriculumAssist curriculumAssist = curriculumAssistMapper.selectById(id);                     //获取课表助教对象
        String collegeName = collegeService.getCollegeName(curriculumAssist.getCollegeId());           //获取学校名称
        String assistName = userService.getUserName(curriculumAssist.getAssistId());                   //获取助教名称
        CurriculumAssistVO curriculumAssistVO = new CurriculumAssistVO();                              //初始化VO
        BeanUtils.copyProperties(curriculumAssist, curriculumAssistVO);                                 //对象复制赋值
        curriculumAssistVO.setCollegeName(collegeName);                                                //设置学校名称
        curriculumAssistVO.setAssistName(assistName);                                                  //设置助教名称
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", curriculumAssistVO);                //成功返回数据对象
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/curriculumAssist", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "curriculumId", value = "课表id", paramType = "query", required = true),
            @ApiImplicitParam(name = "assistId", value = "助教id", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         BigInteger curriculumId,
                         BigInteger assistId,
                         BigInteger byWho) {
        CurriculumAssist curriculumAssist = new CurriculumAssist();               //初始化数据模型
        curriculumAssist.setCollegeId(collegeId);                                 //设置学校id
        curriculumAssist.setCurriculumId(curriculumId);                           //设置课表id
        curriculumAssist.setAssistId(assistId);                                   //设置助教id
        curriculumAssist.setByWho(byWho);                                         //设置操作者
        curriculumAssistMapper.insert(curriculumAssist);                          //执行插入
        return ZQUResult.success();                                               //操作成功返回
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/curriculumAssist/{id}", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "curriculumId", value = "课表id", paramType = "query", required = true),
            @ApiImplicitParam(name = "assistId", value = "助教id", paramType = "query", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            BigInteger curriculumId,
                            BigInteger assistId,
                            BigInteger byWho) {
        CurriculumAssist curriculumAssist = curriculumAssistMapper.selectById(id);//先查出旧记录
        curriculumAssist.setByWho(byWho);                                         //修改操作者
        curriculumAssistMapper.updateById(curriculumAssist);                      //更新操作者
        curriculumAssistMapper.deleteById(id);                                    //逻辑删除
        curriculumAssist = new CurriculumAssist();                                //初始化数据模型
        curriculumAssist.setCollegeId(collegeId);                                 //设置学校id
        curriculumAssist.setCurriculumId(curriculumId);                           //设置课表id
        curriculumAssist.setAssistId(assistId);                                   //设置助教id
        curriculumAssist.setByWho(byWho);                                         //设置操作者
        curriculumAssist.setLastVersion(id);                                      //设置上一版本
        curriculumAssistMapper.insert(curriculumAssist);                          //执行插入
        return ZQUResult.success();                                               //操作成功返回
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/curriculumAssist/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        CurriculumAssist curriculumAssist = curriculumAssistMapper.selectById(id);//先查出旧记录
        curriculumAssist.setByWho(byWho);                                         //修改操作者
        curriculumAssistMapper.updateById(curriculumAssist);                      //更新操作者
        curriculumAssistMapper.deleteById(id);                                    //逻辑删除
        return ZQUResult.success();                                               //操作成功返回
    }
}

